package com.yunqiao.api.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.yunqiao.api.annotation.Login;
import com.yunqiao.common.exception.CustomApiException;
import com.yunqiao.common.utils.ApiResponse;
import com.yunqiao.common.utils.PageUtils;
import com.yunqiao.common.validator.ValidatorUtils;
import com.yunqiao.service.api.RegionService;
import com.yunqiao.service.api.entity.RegionEntity;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * 行政区域表
 *
 * @author Crocutax
 * @email wangxw725@163.com
 * @date 2019-03-08 11:20:27
 */
@RestController
@RequestMapping("api/region")
@Api(tags = "RegionApiController", description = "行政区域表管理")
public class RegionApiController {

    @Autowired
    private RegionService regionService;

    /**
     * 列表
     */
    @ApiImplicitParams({
        @ApiImplicitParam(name = "page", value = "页码", required = false, defaultValue = "1", paramType = "query", dataType = "int"),
        @ApiImplicitParam(name = "limit", value = "数量", required = false, defaultValue = "10", paramType = "query", dataType = "int")
    })
    @GetMapping("/list")
    @ApiOperation("查询行政区域表列表")
    public ApiResponse<PageUtils<RegionEntity>> list(@ApiIgnore @RequestParam Map<String, Object> params) {
        PageUtils page = regionService.queryPage(params);
        return new ApiResponse<>(page);
    }

    /**
     * 信息
     */
    @GetMapping("/info/{id}")
    @ApiOperation("查询单个行政区域表")
    public ApiResponse<RegionEntity> info(@PathVariable("id") Integer id) {
        if (id == null) {
            throw new CustomApiException("ID不能为空");
        }
        RegionEntity region = regionService.getById(id);
        return new ApiResponse<>(region);
    }

    /**
     * 保存
     */
    @Login
    @PostMapping("/save")
    @ApiOperation("保存行政区域表")
    public ApiResponse save(RegionEntity region) {
        ValidatorUtils.validateEntity(region);
        regionService.save(region);
        return new ApiResponse<>();
    }

    /**
     * 修改
     */
    @Login
    @PutMapping("/update/{id}")
    @ApiOperation("修改行政区域表")
    public ApiResponse update(@PathVariable Integer id, RegionEntity region) {
        RegionEntity dbItem = regionService.getById(id);
        if (dbItem == null) {
            throw new CustomApiException("ID有误, 未找到对应数据");
        }
        regionService.updateById(region);
        return new ApiResponse<>();
    }

    /**
     * 删除
     */
    @Login
    @DeleteMapping("/delete/{id}")
    @ApiOperation("删除行政区域表")
    public ApiResponse delete(@PathVariable Integer id) {
        if (id == null) {
            throw new CustomApiException("ID不能为空");
        }
        regionService.removeById(id);
        return new ApiResponse<>();
    }

    @GetMapping("getProvince")
    @ApiOperation("获取省列表")
    public ApiResponse<List<Map<String, Object>>> getProvince() {


        List<RegionEntity> list = this.regionService.list(new QueryWrapper<RegionEntity>().eq("pid", 0));
        List<Map<String, Object>> l = new ArrayList<>();
        if (list != null) {
            list.stream().forEach(regionEntity -> {
                Map<String, Object> map = new HashMap<>();
                map.put("label", regionEntity.getName());
                map.put("value", regionEntity.getId());
                l.add(map);
            });
        }
        return new ApiResponse<>(l);
    }


    @GetMapping("getCity")
    @ApiOperation("获取市列表")
    @ApiImplicitParam(name = "pid", value = "省id")
    public ApiResponse<List<Map<String, Object>>> getCity(Integer pid) {
        List<RegionEntity> list = this.regionService.list(new QueryWrapper<RegionEntity>().eq("pid", pid));
        List<Map<String, Object>> l = new ArrayList<>();
        if (list != null) {
            list.stream().forEach(regionEntity -> {
                Map<String, Object> map = new HashMap<>();
                map.put("label", regionEntity.getName());
                map.put("value", regionEntity.getId());
                l.add(map);
            });
        }
        return new ApiResponse<>(l);
    }

    @GetMapping("getDistrict")
    @ApiOperation("获取市列表")
    @ApiImplicitParam(name = "pid", value = "市id")
    public ApiResponse<List<Map<String, Object>>> getDistrict(Integer pid) {
        List<RegionEntity> list = this.regionService.list(new QueryWrapper<RegionEntity>().eq("pid", pid));
        List<Map<String, Object>> l = new ArrayList<>();
        if (list != null) {
            list.stream().forEach(regionEntity -> {
                Map<String, Object> map = new HashMap<>();
                map.put("label", regionEntity.getName());
                map.put("value", regionEntity.getId());
                l.add(map);
            });
        }
        return new ApiResponse<>(l);
    }
}
